﻿#include "settingwidget.h"
#include "ui_settingwidget.h"

#include <QMessageBox>
#include <QStandardPaths>

#pragma execution_character_set("utf-8")

SettingWidget::SettingWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SettingWidget)
{
    ui->setupUi(this);
    if(Explore::enableOptimize){
        ui->enableOptimizeSetting->setChecked(true);
    }

    ui->defaultUrlSetting->setText(Explore::defaultLoadUrl);

}

SettingWidget::~SettingWidget()
{
    delete ui;
}

void SettingWidget::paintEvent(QPaintEvent *event)
{
    setAutoFillBackground(true);
    updateUi();
}

void SettingWidget::updateUi()
{
    QWidget *p = dynamic_cast<QWidget*>(parent());
    move(10, 120);
}

void SettingWidget::on_pushButton_clicked()
{
    hide();
}

void SettingWidget::on_enableOptimizeSetting_stateChanged(int state)
{
    qDebug()<<state;
    if(state == 2){
        Explore::enableOptimize = true;
    }else{
        Explore::enableOptimize = false;
    }
}

void SettingWidget::on_cacheDeleteSetting_clicked()
{
    // 清除指定目录下的缓存文件
    QString cachePath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
    qDebug() << "cache dir = "<<cachePath;
    // 删除缓存目录下的文件
    // 建议删除的缓存文件：
    // 应用数据路径\cache\QtWebEngine\Default下的所有文件夹
    // 应用数据路径\QtWebEngine\Default\Local Storage的所有文件夹
    Utils::deletePathFile("cache/QtWebEngine/Default");
    Utils::deletePathFile("QtWebEngine/Default/Local Storage");
    // 清除完成提示
    emit chcaeDeleteFinidhed();
}


void SettingWidget::on_historyDeleteSetting_clicked()
{
    QMessageBox msgBox;
    msgBox.setText("delete explore history");
    msgBox.setInformativeText(tr("continue ?"));
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Ok);
    int ret = msgBox.exec();
    if(ret == QMessageBox::Ok){
        // 删除
        Utils::deleteHistroy();
    }
}

void SettingWidget::on_defaultUrlSetting_textChanged(const QString &arg1)
{
    Explore::defaultLoadUrl = arg1;
}

void SettingWidget::on_bookMarkDeleteSetting_clicked()
{
    QMessageBox msgBox;
    msgBox.setText("确定要清空收藏夹吗？");
    msgBox.setInformativeText(tr("continue ?"));
    msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Ok);
    int ret = msgBox.exec();
    if(ret == QMessageBox::Ok){
        // 删除
        Utils::deleteBookMark();
    }
}
